package com.zxh.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zxh.dao.EducationDao;
import com.zxh.dao.ResumeDao;
import com.zxh.entity.Education;
import com.zxh.entity.ResumeBasicInfo;

@WebServlet("/resume.do")
public class ResumeServlet extends HttpServlet{
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
				doPost(req, resp);
		
		}
		
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			String type = req.getParameter("type");//表示操作类别
			HttpSession session =req.getSession();
			Object appObject_id = session.getAttribute("applicant_id");//获得登录时的ID号
			ResumeDao resumedao  = new ResumeDao();
			
			if("add".equals(type)){
				ResumeBasicInfo basicinfo = requestGetData(req);
				int basicInfo_id = resumedao.add(basicinfo,Integer.parseInt(appObject_id.toString()));
				session.setAttribute("basicinfoId", basicInfo_id);
				ResumeBasicInfo resumeInfo  = resumedao.queryResuemBasicInfo(basicInfo_id);//获取简历基本信息
				EducationDao eduDao = new EducationDao();
				List<Education>edulist=eduDao.getEducationList((Integer)appObject_id);//这个集合需要返到页面
				//存简历基本信息对象放到req对象中
				req.setAttribute("resuemInfo", resumeInfo);
				req.setAttribute("edulist", edulist);
				req.getRequestDispatcher("applicant/resume.jsp").forward(req, resp);  
			}else if("query".equals(type)){
				//进入个人简历中心
				int basicInfo_id=0;
				if(session.getAttribute("basicinfoId")!=null){
					basicInfo_id=(Integer)session.getAttribute("basicinfoId");
				}
				int applicant_id=0;
				if(session.getAttribute("applicant_id")!=null){
					applicant_id = (Integer) session.getAttribute("applicant_id");
				}
				EducationDao eduDao = new EducationDao(); 
				List<Education> edulist = eduDao.getEducationList(applicant_id);//这个集合返回到页面
				req.setAttribute("edulist", edulist);
				
				
				
				ResumeBasicInfo resumeInfo=resumedao.queryResuemBasicInfo(basicInfo_id);
				req.setAttribute("resuemInfo", resumeInfo);
				System.out.println(resumeInfo.getHeadShot());
				req.getRequestDispatcher("applicant/resume.jsp").forward(req, resp);
			}else if("modify".equals(type)){
				int basicInfo_id=0;
				if(session.getAttribute("baseinfo_id")!=null){
					basicInfo_id=(Integer)session.getAttribute("baseinfo_id");
					
				}
				ResumeBasicInfo basicinfo=getRequestModifyData(req);
				resumedao.update(basicinfo,basicInfo_id);
				resp.sendRedirect("resume.do?type=query");
				
			}else if("update".equals(type)){
				int basicInfo_id = 0;
				if(session.getAttribute("baseinfo_id")!=null){
					basicInfo_id = (Integer)session.getAttribute("baseinfo_id");
				}	
				ResumeBasicInfo resumeInfo = resumedao.queryResuemBasicInfo(basicInfo_id);
				//存简历基本信息对象放到req对象中
				req.setAttribute("resumeInfo", resumeInfo);
				req.getRequestDispatcher("applicant/resumeBasicinfoUpdate.jsp").forward(req, resp);
			}


			
		}
			
		private ResumeBasicInfo getRequestModifyData(HttpServletRequest req) {
			// TODO Auto-generated method stub
			return null;
		}

		//获取客户端传递的数据
		private ResumeBasicInfo requestGetData(HttpServletRequest req){
			//获取数据的封装
			String realName =req.getParameter("realName");//姓名
			
			String gender=req.getParameter("gender");// 性别
			
			String birthday=req.getParameter("birthday"); //出生日期
			
			String telephone =req.getParameter("telephone");//手机
			
			String email =req.getParameter("email"); //邮箱
			
		    String currentLoc =req.getParameter("currentLoc"); ;//当前所在地
			
		    String residentLog =req.getParameter("residentLog"); ; //户口所在地
			
			String jobIntension =req.getParameter("jobIntension");//求职意向
			
			String jobExperience=req.getParameter("jobExperience");//工作经验
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date birthdayDate=null;
			try {
				birthdayDate = sdf.parse(birthday);
			} catch (Exception e) {
				e.printStackTrace();
			}
			ResumeBasicInfo resumeBasicinfo = new ResumeBasicInfo(realName, gender,birthdayDate,
					currentLoc, residentLog, telephone,
					email, jobIntension, jobExperience); 
			
			return resumeBasicinfo;
		}
		

	
}
